package com.zy.nft_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.nft_system.domain.Antique;
import com.zy.nft_system.domain.Qntiqueauth;
import com.zy.nft_system.domain.Result;
import com.zy.nft_system.domain.Userantique;
import com.zy.nft_system.exception.MyException;
import com.zy.nft_system.mapper.QntiqueauthMapper;
import com.zy.nft_system.mapper.UserantiqueMapper;
import com.zy.nft_system.service.AntiqueService;
import com.zy.nft_system.mapper.AntiqueMapper;
import com.zy.nft_system.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 这是那头猪？
* @description 针对表【t_antique(藏品信息表)】的数据库操作Service实现
* @createDate 2022-12-06 14:31:04
*/
@Slf4j
@Service
public class AntiqueServiceImpl extends ServiceImpl<AntiqueMapper, Antique>
    implements AntiqueService{

    @Resource
    AntiqueMapper antiqueMapper;
    @Resource
    QntiqueauthMapper qntiqueauthMapper;

    @Resource
    UserantiqueMapper userantiqueMapper;

    @Transactional(rollbackFor = MyException.class,propagation = Propagation.REQUIRED)
    @Override
    public Result insertCollectio(Antique antique) {
        //添加藏品信息
        int i =antiqueMapper.insertCollectio(antique);
        if (i != 1) {
            log.info("未知原因添加失败");
            throw new MyException("未知原因添加失败");
        }
        Antique selectOne = antiqueMapper.selectOne(new QueryWrapper<Antique>().eq("name", antique.getName()));
        //添加藏品权限信息
        Qntiqueauth qntiqueauth = new Qntiqueauth();
        qntiqueauth.setAid(selectOne.getId());
        qntiqueauth.setCreatetime(DateUtils.dataFormat(new Date()));
        //默认不能寄售
        qntiqueauth.setConsignment(0);
        //默认不能转增
        qntiqueauth.setIncrease(0);
        int insert = qntiqueauthMapper.insert(qntiqueauth);
        if (insert != 1) {
            log.info("未知原因添加失败");
            throw new MyException("未知原因添加失败");
        }
        log.info("藏品信息和藏品权限信息添加成功");
        return Result.success("藏品添加成功",null);
    }

    @Override
    public Result queryCollection(String name) {
        log.info("查询藏品信息成功");
        List<Antique> antiques = antiqueMapper.queryCollection(name);
        return Result.success("查询成功",antiques);
    }

    @Override
    @Transactional(rollbackFor = MyException.class,propagation = Propagation.REQUIRED)
    public Result updataCollectio(Antique antique) {
        //修改藏品消息
        log.info("正在修改藏品信息");
        antique.setModiftime(DateUtils.dataFormat(new Date()));
        int i =antiqueMapper.updataCollectio(antique);
        if (i != 1) {
            log.info("未知原因修改失败");
            throw new MyException("未知原因修改失败");
        }
        log.info("查询是否有用户有此藏品");
        List<Userantique> userantiques = userantiqueMapper.selectList(new QueryWrapper<Userantique>().eq("aid", antique.getId()));

        if (userantiques.isEmpty()){
            log.info("没有用户有此藏品，修改成功");
            return Result.success("藏品修改成功",null);
        }
        log.info("有用户有此藏品");
        for (Userantique userantique : userantiques) {
            userantique.setName(antique.getName());
            userantique.setImgsrc(antique.getImgsrc());
            userantique.setModiftime(DateUtils.dataFormat(new Date()));
            //修改用户藏品信息
            log.info("正在修改用户藏品信息");
            int aid = userantiqueMapper.update(userantique, new UpdateWrapper<Userantique>().eq("aid", antique.getId()));
            if (aid != 1) {
                log.info("未知原因修改失败");
                throw new MyException("未知原因修改失败");
            }
        }
        return Result.success("藏品修改成功",null);
    }
}




